package com.crm.system.controller.MessageManager;

import com.crm.common.model.DataResponse;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.DhResponse;
import com.crm.common.model.Enum.ResponseCodeEnum;

import com.crm.model.entity.MessageManager.Message;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.entity.sys.permissionManagement.SysCompany;
import com.crm.model.qo.messageManager.MarkReadQO;
import com.crm.model.qo.messageManager.MessageAddQO;
import com.crm.model.qo.messageManager.MessageQO;

import com.crm.model.qo.messageManager.ReplyQO;
import com.crm.model.vo.messageManager.MessageVO;
import com.crm.model.vo.user.UserAccountManagerVO;
import com.crm.system.service.messageManager.MessageService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


/**
 * 信息
 */
@RestController
@RequestMapping("/message")
@Slf4j
public class MessageController {

    @Autowired
    private MessageService messageService;
    private DhPageInfo<MessageVO> list;


    /**
     * 信息列表
     * @return
     */
    @PostMapping("/selectMessageList")
    public DataResponse selectMessageList(HttpServletRequest request,
                                          @RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                          @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                          @RequestParam(value="sortName",required = false) String sortName,//排序列名字
                                          @RequestParam(value="sortOrder",required = false) String sortOrder,//排序规则(asc/desc)
                                          MessageQO messageQO//查询条件
    ){
        try{
            DhPageInfo<MessageVO> list = messageService.selectMessageList(pageIndex,pageSize,messageQO,sortName,sortOrder);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),list);
        }catch(Exception e){
            log.error("message list error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 标记操作(已读和删除)
     * @param qo
     * @return
     */
    @PostMapping("/markRead")
    public DataResponse markRead(@RequestBody MarkReadQO qo) {
        Boolean result = messageService.markRead(qo);
        if (result) {
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        } else {
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 回复
     * @param message
     * @return
     */
    @PostMapping("/replyMessage")
    public DataResponse replyMessage(@RequestBody ReplyQO message) {
        Boolean result = messageService.replyMessage(message);
        if (result) {
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        } else {
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 消息详情
     * @param messageId 消息id
     * @param msgType 1已发消息  2为已读,未读,审批,回款,系统
     * @return
     */
    @PostMapping("/messageDetail")
    public DataResponse messageDetail(@RequestParam(value="messageId",required = false) String messageId,
                                      @RequestParam(value="msgType",required = false) String msgType) {
        Map<String,Object> map = messageService.messageDetail(messageId,msgType);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),map);
    }

    /**
     * 删除消息
     * @param themeId 主题id
     * @return
     */
    @PostMapping("/deleteThemeId")
    public DataResponse deleteThemeId(@RequestParam(value="themeId",required = false) String themeId) {
        Boolean result = messageService.deleteThemeId(themeId);
        if (result) {
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        } else {
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 标记为重要
     * @param messageId 消息id
     * @return
     */
    @PostMapping("/markImportant")
    public DataResponse markImportant(@RequestParam(value="messageId",required = false) String messageId) {
        Boolean result = messageService.markImportant(messageId);
        if (result) {
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        } else {
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 新增消息
     * @param qo
     * @return
     */
    @PostMapping("/addMessage")
    public DataResponse markImportant(@RequestBody MessageAddQO qo) {
        Boolean result = messageService.addMessage(qo);
        if (result) {
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        } else {
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 公司列表
     * @return
     */
    @PostMapping("/selectCompanyList")
    public DataResponse selectCompanyList() {
        List<SysCompany> list = messageService.selectCompanyList();
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),list);
    }

    /**
     * 查询用户
     * @return
     */
    @PostMapping("/selectUserList")
    public DataResponse selectUserList(@RequestParam(value="companyId",required = false) String companyId) {
        List<UserAccountManagerVO> list = messageService.selectUserList(companyId);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),list);
    }

    /**
     * 删除单条消息
     * @param messageId 消息id
     * @return
     */
    @PostMapping("/deleteByMessageId")
    public DataResponse deleteByMessageId(@RequestParam(value="messageId",required = false) String messageId) {
        Boolean result = messageService.deleteByMessageId(messageId);
        if (result) {
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        } else {
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }







}

